/**
 * \file clamping.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_CLAMPING_HPP_INCLUDED
#define BASIS_CLAMPING_HPP_INCLUDED

namespace basis
{

	//------------------------------------------------------------
	// Clamping functions
	//------------------------------------------------------------

	/**
	 * Clamps a number to a given range.
	 *
	 * \tparam Real A real number type.
	 *
	 * \param value The number to clamp.
	 * \param min The minimum value to use.
	 * \param max The maximum value to use.
	 * \returns The number clamped to the range [min, max].
	 */
	template <typename Real>
	inline Real clamp(Real value, Real min, Real max)
	{
		if (value >= max)
			return max;
		else if (value <= min)
			return min;
		else
			return value;
	}

	/**
	 * Clamps a vector's components to a given range.
	 *
	 * \relates vector3
	 *
	 * \tparam Real A real number type.
	 * \tparam InstructionSet The SIMD instruction set to use.
	 *
	 * \param value The vector to clamp.
	 * \param min The minimum components.
	 * \param max The maximum components.
	 * \returns The vector clamped to the components.
	 */
	template <typename Real, typename InstructionSet>
	inline vector3<Real, InstructionSet> clamp(const vector3<Real, InstructionSet>& value, const vector3<Real, InstructionSet>& min, const vector3<Real, InstructionSet>& max)
	{
		return vector3<Real, InstructionSet>(
			clamp(value.x(), min.x(), max.x()),
			clamp(value.y(), min.y(), max.y()),
			clamp(value.z(), min.z(), max.z()));
	}

	//------------------------------------------------------------
	// Saturate functions
	//------------------------------------------------------------

	/**
	 * Clamps a number to the range [0, 1].
	 *
	 * \tparam Real A real number type.
	 *
	 * \param value The number to clamp.
	 * \returns The number clamped to the range [0, 1].
	 */
	template <typename Real>
	inline Real saturate(Real value)
	{
		if (value >= (Real)1)
			return (Real)1;
		else if (value <= (Real)0)
			return (Real)0;
		else
			return value;
	}

	/**
	 * Clamps the components of the vector to the range [0, 1].
	 *
	 * \relates vector3
	 *
	 * \tparam Real A real number type.
	 * \tparam InstructionSet The SIMD instruction set to use.
	 *
	 * \param value The vector to clamp.
	 * \returns The vector whose components are clamped to the range [0, 1].
	 */
	template <typename Real, typename InstructionSet>
	inline vector3<Real, InstructionSet> saturate(const vector3<Real, InstructionSet>& value)
	{
		return vector3<Real, InstructionSet>(
			saturate(value.x()),
			saturate(value.y()),
			saturate(value.z()));
	}

} // end namespace basis

#endif // end BASIS_CLAMPING_HPP_INCLUDED
